import React from 'react';
import { Modal, Form, Input, message, Select } from 'antd';
import { createUnit } from '../../services/unit';

interface AddUnitModalProps {
    visible: boolean;
    onCancel: () => void;
    onSuccess: () => void;
}

const { Option } = Select;

const AddUnitModal: React.FC<AddUnitModalProps> = ({ visible, onCancel, onSuccess }) => {
    const [form] = Form.useForm();

    const handleOk = async () => {
        try {
            const values = await form.validateFields();
            await createUnit(values);
            message.success('新建单元成功');
            form.resetFields();
            onSuccess();
        } catch (err: any) {
            message.error(err?.response?.data?.msg || '新建失败');
        }
    };

    return (
        <Modal
            title="新建单元"
            open={visible}
            onOk={handleOk}
            onCancel={() => {
                form.resetFields();
                onCancel();
            }}
            destroyOnClose
        >
            <Form form={form} layout="vertical">
                <Form.Item
                    label="应用ID"
                    name="appId"
                    rules={[
                        { required: true, message: '请输入应用ID' },
                        { pattern: /^\d{6}$/, message: '应用ID必须为6位纯数字' }
                    ]}
                >
                    <Input maxLength={6} />
                </Form.Item>
                <Form.Item
                    label="单元ID"
                    name="unitId"
                    rules={[
                        { required: true, message: '请输入单元ID' },
                        { pattern: /^[A-Za-z0-9-]+$/, message: '单元ID只能包含字母、数字和-，不能有其他特殊字符' }
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="制品路径"
                    name="artifactoryPath"
                    rules={[]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="单元类型"
                    name="unitType"
                    rules={[{ required: true, message: '请选择单元类型' }]}
                >
                    <Select placeholder="请选择单元类型">
                        <Option value="Springmvc">Springmvc</Option>
                        <Option value="DUBBO">DUBBO</Option>
                        <Option value="pipeline">pipeline</Option>
                        {/* 可根据实际类型补充 */}
                    </Select>
                </Form.Item>
                <Form.Item
                    label="开发团队"
                    name="ownTeam"
                    rules={[{ required: true, message: '请输入开发团队' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="归属中心"
                    name="ownCenter"
                    rules={[]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="git仓库地址"
                    name="gitRepoUrl"
                    rules={[{ required: true, message: '请输入git仓库地址' }]}
                >
                    <Input />
                </Form.Item>
            </Form>
        </Modal>
    );
};

export default AddUnitModal; 